<?php
namespace App\Http\Controllers\Business\Common;

use App\Models\City;
use App\Models\Comment;
use App\Models\Order;
use App\Models\RedEnvelope;
use App\Models\Shop;
use App\Models\ShopSalesman;
use App\Models\SmsMessage;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class RedEnvelopeManagement
{
    // 1.怎么获取：注册，充值，订单，banner
    // 2.红包领取方式：自动，手动，分享
    // 3.红包发放形式：可以支持多个／单个红包，
    // 4.红包的使用条件：指定地区，是否可以支持多个红包，指定商家，指定品类，满多少元，无限制
    // 5.代理商发放的流程： 先到总部购买红包，然后总部分配金额给代理商，代理商逾期的红包总部给予补偿；
    // 6.

    //通过订单号获得红包详情
    public static function getOrderRedEnvelope($userId, $cityId, $orderNumber) {
        if(!$cityId||!$userId||!$orderNumber) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $data = [];
//        $list = [
//            'redenvelopeid'=>1,
//            'amount' => 8.60,
//            'minimumamount' => 0.00,
//            'endtime' => '2017-03-15',
//            'usetime' => '',
//            'status' => 0,
//            'cityid' => 1,
//            'cityname' => '南昌市',
//            'firstclassid' => 0,
//            'secondclassid' => 0,
//            'firstclassname' => '',
//            'secondclassname' => ''
//        ];
        $redEnvelope = Order::where('orders.ordernumber', $orderNumber)->where('orders.userid', $userId)
            ->join('red_envelopes', 'orders.bonusid', '=', 'red_envelopes.id')
            ->select('red_envelopes.id as redenvelopeid', 'red_envelopes.amount', 'red_envelopes.minimumamount',
                'red_envelopes.endtime', 'red_envelopes.usetime', 'red_envelopes.cityid', 'red_envelopes.firstclassid',
                'red_envelopes.secondclassid', 'red_envelopes.firstclassname','red_envelopes.secondclassname')
            ->first();
        if($redEnvelope) {
            $redEnvelope['cityname'] = City::where('id', $redEnvelope['cityid'])->pluck('name')->first();
            array_push($data, $redEnvelope);
        }
        return ['status'=>'success', 'data'=>$data];
    }

    //红包详情
    public static function redEnvelopeDetail() {

    }

    //使用红包
    public static function useRedEnvelope() {

    }

    //发放红包
    public static function grantRedEnvelope() {

    }
}